package com.yzs.core.http

import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.EncryptUtils

/**
 *    author : yzs
 *    date   : 2022/1/1211:50
 *    desc   : 处理请求 公共参数封装
 */


val MANUFACTURER = android.os.Build.MANUFACTURER// 获取厂商名
val MODEL = android.os.Build.MODEL// 获取手机型号
val RELEASE = android.os.Build.VERSION.RELEASE// 获取Android 例 = 11

object ParamUtils {

    fun getPublicParams(): MutableMap<String, String> {

        val uniqueDeviceId = DeviceUtils.getUniqueDeviceId() // 设备唯一标识
        val map = mutableMapOf<String, String>()
        map["mm"] = "$MANUFACTURER$MODEL$RELEASE" // 手机信息
        map["imtoken"] = ""
        map["osid"] = "android"
        map["ver"] = AppUtils.getAppVersionName()
        map["verCode"] = AppUtils.getAppVersionCode().toString()
        map["app"] = AppUtils.getAppPackageName()
        map["timestamp"] = System.currentTimeMillis().toString()
        map["deviceId"] = uniqueDeviceId ?: ""
        return map
    }

    fun buildParam(map: MutableMap<String, String>? = null): MutableMap<String, String> {
        val publicParams = getPublicParams()
        if (!map.isNullOrEmpty()) publicParams.putAll(map)
        val param = publicParams.toSortedMap()
        val builder = StringBuilder()
        param.forEach { builder.append(it.key).append("=").append(it.value).append("&") }
        builder.append("key=").append("C.HTTP_KEY_VALUE")
        val s = builder.toString()
        param["sign"] = EncryptUtils.encryptMD5ToString(s)
        return param
    }
}
